package SingletonPattern;

//双重检查加锁  
public class Singleton3 {

	//volatile 确保多线程时，获取最新的instance java1.5才可以用的关键字
	private volatile static Singleton3 instance;
	
	private Singleton3(){}
	
	public static Singleton3 getInstance(){
		if(instance == null){
			//instance为空时 同步
			synchronized(Singleton3.class){
				if(instance == null){
					instance = new Singleton3();
				}
			}
		}
		return instance;
	}
	
	public static void main(String[] args) {
		Singleton3 s1 = Singleton3.getInstance();
		Singleton3 s2 = Singleton3.getInstance();
		System.out.println(s1 == s2);
	}

}
